//
//  StatusPictureView.swift
//  新浪weibo
//
//  Created by test on 15/8/6.
//  Copyright © 2015年 test. All rights reserved.
//

import UIKit
import SDWebImage

class StatusPictureView: UICollectionView {
    
    var status: Status? {
        didSet {
            sizeToFit()
            reloadData()
        }

    }
    
    override func sizeThatFits(size: CGSize) -> CGSize {
        return calcViewSize()
    }
    
    private func calcViewSize() -> CGSize {
        
        let itemSize = CGSize(width: 90, height: 90)
        
        let margin:CGFloat = 10
        
        let rowCount = 3
        
        pictureLayout.itemSize = itemSize
        
        let count = status?.pic_urls?.count ?? 0
        
        if count == 0{
            
            return CGSizeZero
        }
        
        if count == 1 {
            let key = status?.pictureURLs![0].absoluteString
            let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(key)
            var size = CGSize(width: 150, height: 120)
            
            if image != nil {
               size = image.size
            }
            
            size.width = size.width < 40 ? 40 : image.size.width
            size.width = size.width > UIScreen.mainScreen().bounds.width ? 150 : size.width
            pictureLayout.itemSize = size
            return size
            
        }
        
        if count == 4 {
            let w = 2 * itemSize.width + CGFloat(margin)
      
            return CGSize(width: w, height: w)
            
        }
        
        // 计算行数
        let row = (count - 1) / rowCount + 1
        let w = itemSize.width * CGFloat(rowCount) + margin * CGFloat(rowCount - 1)
        let h = itemSize.height * CGFloat(row) + margin * CGFloat(row - 1)
        
        return CGSize(width: w, height: h)
        
    }
    
    
    private let pictureLayout = UICollectionViewFlowLayout()

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: pictureLayout)
        backgroundColor = UIColor.lightGrayColor()
        
        registerClass(StatusPictureViewCell.self, forCellWithReuseIdentifier: "cell")
        
        self.dataSource = self
        
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension StatusPictureView:UICollectionViewDataSource {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return status?.pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! StatusPictureViewCell
        cell.imageURL = status?.pictureURLs![indexPath.item]
        
        return cell
    }

}

class StatusPictureViewCell: UICollectionViewCell {
    
    var imageURL: NSURL? {
        didSet {
        iconView.sd_setImageWithURL(imageURL!)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
   
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
        contentView.addSubview(iconView)
        
        iconView.ff_Fill(iconView)
        
    }
    
    private lazy var iconView:UIImageView = {
        let iv = UIImageView()
        iv.contentMode = UIViewContentMode.ScaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    
}

